package com.yidiandao.business.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * Activity任务栈
 */
public class ActivityStack {

    private static List<Activity> activityStack = new ArrayList<>();

    private static int appCount;

    public static void addActivity(Activity activity) {
        activityStack.add(activity);
    }

    public static void removeActivity(Activity activity) {
        activityStack.remove(activity);
    }

    public static void onActivityStart() {
        appCount++;
    }

    public static void onActivityStop() {
        appCount--;
    }

    public static Activity getTopActivity() {
        try {
            return activityStack.get(activityStack.size() - 1);
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }

    public static List<Activity> getActivityStack() {
        return activityStack;
    }

    public static void finishAllActivity() {
        for (Activity activity : activityStack) {
            activity.finish();
        }
    }

    public static boolean isForeground() {
        return appCount != 0;
    }
}
